package com.lr.app.config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.ClassPathResource;

import com.lr.falsework.common.constant.SysConstant;

@ConfigurationProperties(prefix = "excel.property")
public class ExcelConfig {

	private String priceChangesExcelModelPath;
	public static File priceChangesExcelModelFile;

	public void setPriceChangesExcelModelPath(String priceChangesExcelModelPath) {
		this.priceChangesExcelModelPath = priceChangesExcelModelPath;
	}

	@PostConstruct
	public void init() throws IOException {
		File f = new File(ExStorageConfig.getExcelTempFolder());
		File[] fAry = f.listFiles();
		if (fAry != null) {
			for (File cf : fAry) {
				if (cf.isFile()) {
					cf.delete();
				}
			}
		}
		if (!f.exists()) {
			boolean mres = f.mkdirs();
			if (!mres) {
				System.out.println("====================资源存储目录创建失败");
				System.out.println("====================资源存储目录创建失败");
				System.out.println("====================资源存储目录创建失败");
			}
		}
		if (priceChangesExcelModelPath.startsWith(SysConstant.FILEPATH_PREFIX)) {
			priceChangesExcelModelPath = priceChangesExcelModelPath.substring(SysConstant.FILEPATH_PREFIX.length());
			priceChangesExcelModelFile = new File(priceChangesExcelModelPath);
		} else if (priceChangesExcelModelPath.startsWith(SysConstant.CLASSPATH_PREFIX)) {
			priceChangesExcelModelPath = priceChangesExcelModelPath.substring(SysConstant.CLASSPATH_PREFIX.length());
			ClassPathResource crs = new ClassPathResource(priceChangesExcelModelPath);
			FileOutputStream of = null;
			InputStream ips = null;
			try {
				priceChangesExcelModelFile = new File(
						ExStorageConfig.getExcelTempFolder() + "//" + new File(priceChangesExcelModelPath).getName());
				of = new FileOutputStream(priceChangesExcelModelFile);
				ips = crs.getInputStream();
				byte[] bt = new byte[1024];
				while (ips.read(bt) != -1) {
					of.write(bt);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (of != null)
					of.close();
				if (ips != null)
					ips.close();
			}
		} else {
			priceChangesExcelModelFile = new File(priceChangesExcelModelPath);
		}
		if (!priceChangesExcelModelFile.exists()) {
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到：" + priceChangesExcelModelPath);
			System.err.println("----------------文件路径没找到，实际path：" + priceChangesExcelModelFile.getCanonicalPath());
		}

	}

	@PreDestroy
	public void destory() {
		File f = new File(ExStorageConfig.getExcelTempFolder());
		f.deleteOnExit();
	}
}
